home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-07-18 | 8.3 KB | 312 lines |
- /*
- * @(#)NSRunner.java 1.8 97/05/22
- *
- * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
- *
- * This software is the confidential and proprietary information of Sun
- * Microsystems, Inc. ("Confidential Information"). You shall not
- * disclose such Confidential Information and shall use it only in
- * accordance with the terms of the license agreement you entered into
- * with Sun.
- *
- * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
- * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
- * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
- * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
- * THIS SOFTWARE OR ITS DERIVATIVES.
- *
- * CopyrightVersion 1.0
- */
-
- package sun.servlet.netscape;
-
- import javax.servlet.*;
- import java.io.*;
- import java.util.Hashtable;
- import java.util.Enumeration;
- import java.util.Properties;
- import sun.servlet.ServletLoader;
- import sun.servlet.util.Pool;
- import netscape.server.applet.HttpApplet;
- import netscape.server.applet.Misconfiguration;
- import netscape.net.URLConnection;
-
- /**
- * Netscape server application for running servlets.
- *
- * @version 1.8, 05/22/97
- * @author David Connelly
- */
- public
- class NSRunner extends HttpApplet implements ServletContext {
- /*
- * The class loaders for all loaded servlets.
- */
- private static Hashtable loaders = new Hashtable();
-
- /*
- * The pool of available request objects.
- */
- private static Pool reqPool = new Pool("sun.servlet.netscape.NSRequest");
-
- /*
- * The pool of available response objects.
- */
- private static Pool resPool = new Pool("sun.servlet.netscape.NSResponse");
-
- /*
- * The class loader for the servlet being invoked.
- */
- private ServletLoader loader;
-
- /*
- * The properties file for servlets.
- */
- private Properties servletProps = new Properties();
-
- /**
- * Constructs a new server application for invoking servlets.
- */
- public NSRunner() {
- }
-
- /**
- * Load servlet properties from servlet prop file
- */
- private void loadServletProps(String servletPropFile) {
-
- File spf = new File(servletPropFile);
- try {
- servletProps.load(new FileInputStream(spf));
- } catch (IOException ioe) {
- log("Could not load servlet properites file "+servletPropFile);
- ioe.printStackTrace();
- }
-
- }
-
- /**
- * Runs the specified servlet.
- */
- public void run() throws Exception {
- // Get servlet directory
- String vpath = getConfigProperty("vpath");
- if (vpath == null) {
- vpath = "/servlet";
- }
- String initfile = getConfigProperty("initfile");
- if (initfile != null) {
- loadServletProps(initfile);
- }
-
- String dir = translateURI(vpath);
- if (dir == null) {
- throw new Misconfiguration("no mapping for " + vpath);
- }
- // Get class loader for directory, and create new loader if not found.
- loader = (ServletLoader)loaders.get(dir);
- if (loader == null) {
- loader = new ServletLoader(dir);
- loaders.put(dir, loader);
- }
- // Get name and path info for servlet being invoked
- String uri = getRequestProperty("uri");
- if (!uri.startsWith(vpath)) {
- throw new Misconfiguration("uri must start with " + vpath);
- }
- int i = vpath.length() + 1;
- if (i > uri.length()) {
- throw new Misconfiguration("no servlet specified");
- }
- int j = uri.indexOf("/", i);
- NSRequest req = (NSRequest)reqPool.alloc();
- req.init(this);
- String name;
- if (j == -1) {
- name = uri.substring(i);
- req.pathInfo = null;
- req.servletPath = uri;
- } else {
- name = uri.substring(i, j);
- req.pathInfo = uri.substring(j);
- req.servletPath = uri.substring(0, j);
- }
- // Get selected servlet and dispatch request
- Servlet s = getServlet(name);
- if (s == null) {
- throw new Misconfiguration("servlet " + name + " not found");
- }
- NSResponse res = (NSResponse)resPool.alloc();
- res.init(this);
- try {
- s.service(req, res);
- } catch (Throwable t) {
- sendDebug(t);
- }
- res.finish();
- // Free up request / response objects after use
- req.reset();
- reqPool.free(req);
- res.reset();
- resPool.free(res);
- }
-
- /**
- * Starts a response for the server application.
- */
- public int startResponse() throws IOException {
- return super.startResponse();
- }
-
- /**
- * Loads a servlet from the current class loader.
- * @param name the Servlet class name
- * @return the Servlet, or null if not found
- * @exception ServletException if an initialization error has occurred
- */
- public Servlet getServlet(String name) throws ServletException {
- // First attempt to resolve the name against the prop file
- String cname = servletProps.getProperty("servlet."+name+".code");
- String init = servletProps.getProperty("servlet."+name+".initArgs");
- try {
- if (cname == null) {
- return loader.loadServlet(name,
- new NSServletConfig( this, name,
- init));
- } else {
- return loader.loadServlet(cname,
- new NSServletConfig( this, cname,
- init));
- }
- } catch (ServletException e) {
- e.printStackTrace();
- return null;
- }
- }
-
- /**
- * Returns an enumeration of all the currently loaded servlets.
- */
- public Enumeration getServlets() {
- return loader.getServlets();
- }
-
- /**
- * Returns the translated path for the specified virtual path.
- * @param path the virtual path
- * @return the translated path
- */
- public String getRealPath(String path) {
- return translateURI(path);
- }
-
- /**
- * Returns the MIME type for the specified file name.
- * @param name the file name
- * @return the MIME type of the file
- */
- public String getMimeType(String name) {
- String type = URLConnection.guessContentTypeFromName(name);
- return type != null ? type : "text/plain";
- }
-
- /**
- * Returns the name of this server. Unfortunately, there is no
- * NSAPI property to query for this so the name is hardcoded here.
- */
- public String getServerInfo() {
- return "Netscape Enterprise/2.x";
- }
-
- /**
- * Logs a message for a servlet.
- * @param msg the log message
- */
- public void log(String msg) {
- inform(msg);
- }
-
- /**
- * Returns an attribute of the server for the specified key name.
- * @param name the attribute name
- */
- public Object getAttribute(String name) {
- return null;
- }
-
- /*
- * Sends exception stack trace to the client for debugging.
- */
- void sendDebug(Throwable t) throws IOException {
- returnErrorResponse("text/html", SERVER_ERROR);
- PrintStream out = getOutputStream();
- t.printStackTrace(out);
- out.flush();
- }
- }
-
- /**
- * This class is used to hold internal configuration information about
- * each servlet called by the server.
- */
- class NSServletConfig implements ServletConfig {
-
- private Properties servletProperties = new Properties();
- private ServletContext context;
- private String servletName;
-
- NSServletConfig(ServletContext context, String servletName,
- String propString) {
- this.context = context;
- this.servletName = servletName;
- if (propString != null) {
- propString = propString.replace(',','\n');
- StringBufferInputStream sbin =
- new StringBufferInputStream(propString);
- String line;
- DataInputStream ds = new DataInputStream(sbin);
- try {
- while ((line = ds.readLine()) != null) {
- int equalTo = line.indexOf("=");
- if (equalTo == -1)
- continue;
- String l = line.substring(0, equalTo);
- String r = line.substring(equalTo + 1, line.length());
- if (l != null && r != null)
- servletProperties.put(l,r);
- }
- } catch (IOException e) {
- System.err.println("Cannot read initial arguments for servlet "
- + servletName + ": ");
- e.printStackTrace(System.err);
- }
- }
- }
-
-
- /**
- * Returns the servlet context.
- */
- public ServletContext getServletContext() {
- return context;
- }
-
- /**
- * Returns a servlet initialization parameter.
- */
- public String getInitParameter(String name) {
- return (String)servletProperties.get(name);
- }
-
- /**
- * Returns an enumeration of strings representing the initialization
- * parameters for this servlet.
- */
- public Enumeration getInitParameterNames() {
- return servletProperties.propertyNames();
- }
-
-
- }
-